require 'scraper-base.rb'
require 'vehicle.rb'

class QatarLivingCarScraper  < ScraperBase

  attr_reader :brand
  
	def initialize(brand)
    super('qatarliving.log')
    @brand = Brand.find_by_id(brand)
	  @site_id = 'qatarlivingcars'
		@remoteBaseURL = 'http://www.qatarliving.com'
		@startURL = "/classifieds/cars?filter0=71&filter1=#{brand}&filter2=&filter3=&filter4=**ALL**"
		@detailBaseURL = 'http://www.qatarliving.com/node'		
	end
	
  def get_total_properties(page)
	  begin
      anchor = page.at("//div[@class='pager']/a[@class='pager-last active']")
	    matches = anchor['href'].match(/page=([\d]+)/)
      @total = (matches[1].to_i + 1) * 20
    rescue
  	  @total = 'Unknown'
    end
  end
  
  def get_next_page_link(page)
    begin
      anchor = page.at("//div[@class='pager']/a[@class='pager-next active']")
      return "#{@remoteBaseURL}#{anchor['href']}"
    rescue
      nil
    end
    nil
  end

  def process_list_page(page)
    #puts page.uri.to_s
    
    (page/"//div[@class='view-content view-content-Classifieds-Car-Search']/div/ul/li").each do |li|
      v = Vehicle.new
      v.brand = self.brand
      anchor = li.at(".//td[@class='view-Classifieds-title']/a")
      v.title = anchor.text
      pline "  Item #{@curr_property} of #{@total}..."
      # get detailed info
      href = anchor['href']
      v.id = href.match(/.*node\/([\d]+)/)[1]
      v.url = "#{@remoteBaseURL}#{href}"
      #puts "processing: #{anchor.text} [#{href}]"
      begin
        if not Vehicle.find_by_id(v.id)
          v = process_detail_page(v)
          v.save
        end
      rescue => exception
        pline("Exception thrown while processing #{v.url}", true)
        puts exception
        puts exception.backtrace
      end
      @curr_property = @curr_property + 1
    end
  end
  
	def process_detail_page(vehicle)
    page = @agent.get vehicle.url

	  #page.save_as("detailed.html")

    datetime = node_text(page, "//span[@class='submitted']")
    m = datetime.match(/By.*\s*on\s*[\w]{3},\s*([\d\/]+)\s*-\s*(.*)/)
    vehicle.submit_date = m[1]
    vehicle.submit_time = m[2]
    
    key = nil
    (page/"//div[@class='node']/div[@class='content']/node()").each do |node|
      t = node.text
      if t.rindex(":") == t.length-1
        key = t.delete(":")
      else
        if key != nil and t != ""
          val = t
          #puts "#{key} = #{val}"
          case key.downcase
          when "model"
            vehicle.model = val
          when "mileage"
            vehicle.mileage = val
          when "price"
            vehicle.price = val
          when "contact number"
            vehicle.contact_number = val
          when "vehicle type"
            vehicle.vtype = val
          when "description"
            vehicle.description = val
          else
            vehicle.others = "#{vehicle.others}\n---\n#{val}"
          end
          key = nil
        end
      end
    end
    vehicle
	end
  
end

